package com.shengyijie.activity.share;
 
import com.shengyijie.activity.R;

import android.app.Activity;
import android.app.ProgressDialog; 
import android.content.Intent;
import android.graphics.Bitmap; 
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.net.Uri;
import android.net.http.SslError; 
import android.webkit.SslErrorHandler;

public class ShareLogin extends Activity implements OnClickListener{

	public WebView web;  
	private ProgressBar myProgressBar; 
	private int type; 
	 String urls;
	 boolean islongined=false;
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
	        setContentView(R.layout.sharelogin);
	        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,      
                 R.layout.title);	        
	        Button back=(Button) this.findViewById(R.id.back);
	        back.setVisibility(View.VISIBLE);
	        back.setOnClickListener(this);	 
	       TextView tv=(TextView) this.findViewById(R.id.title);
	        if(ShareConstant.share_type==ShareConstant.SHARE_SINA)
	           tv.setText("新浪微博登录认证");
	        else if(ShareConstant.share_type==ShareConstant.SHARE_TENCNT)
		       tv.setText("腾讯微博登录认证");
	        else if(ShareConstant.share_type==ShareConstant.SHARE_RENREN)
			   tv.setText("人人网登录认证");
	        
	        
	        myProgressBar = (ProgressBar) findViewById(R.id.progressbar);
	        web = (WebView)this.findViewById(R.id.web);	        
	        web.getSettings().setJavaScriptEnabled(true);
	        web.getSettings().setLightTouchEnabled(true);
	        web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
	        web.getSettings().setDatabaseEnabled(true);  
	        web.getSettings().setBuiltInZoomControls(true);
	        web.getSettings().setSupportZoom(true);
	        web.setInitialScale(100);
	        web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
	        
	        urls=this.getIntent().getStringExtra("uri");
	        type=this.getIntent().getIntExtra("type",1);
	          
            
	        web.setWebViewClient(new WebViewClient(){
	            public boolean shouldOverrideUrlLoading(WebView view, String url) {  
	                                            view.loadUrl(url);  
	                                            return true;  
	            }
	            @Override
				public void onPageFinished(WebView view, String url) {
	            	myProgressBar.setVisibility(View.GONE);
	            	if(islongined)
	            		web.setVisibility(View.GONE);
					super.onPageFinished(view, url);
				}

				@Override
				public void onPageStarted(WebView view, String url, Bitmap favicon) {
					   super.onPageStarted(view, url, favicon);
					    myProgressBar.setVisibility(View.VISIBLE);
						if(url.indexOf("OAuthActivity?oauth_token")>0){
							  Bundle bd = new Bundle();
							  url=url.replace('#', '?');
							  Uri uri=Uri.parse(url); 
				              bd.putString("token", uri.getQueryParameter("oauth_verifier"));
				              bd.putString("oauth_verifier", "");
				              bd.putInt("type", type);
				              bd.putString("state", "ok");	
				              islongined=true;
				              web.setVisibility(View.GONE);
				              Intent it = new Intent();
				              it.putExtras(bd);
				              setResult(RESULT_OK, it);
				              ShareLogin.this.finish(); 
					    }else if(url.indexOf("?code")>0){
							  Bundle bd = new Bundle();
							  url=url.replace('#', '?');
							  Uri uri=Uri.parse(url);
				              bd.putString("token", uri.getQueryParameter("code"));
				              bd.putString("oauth_verifier", "");
				              bd.putInt("type", type);
				              bd.putString("state", "ok");
				              islongined=true;
				              Intent it = new Intent();
				              it.putExtras(bd);
				              setResult(RESULT_OK, it);
				              ShareLogin.this.finish();
						}else if(url.indexOf("QWeiboShengYiJie")>0){
							  Bundle bd = new Bundle();
							  url=url.replace('#', '?');
							  Uri uri=Uri.parse(url);
				              bd.putString("token", uri.getQueryParameter("oauth_token"));
				              bd.putString("oauth_verifier", uri.getQueryParameter("oauth_verifier"));
				              bd.putInt("type", type);
				              bd.putString("state", "ok");	
				              Intent it = new Intent();
				              it.putExtras(bd);
				              islongined=true;
				              setResult(RESULT_OK, it);
				              ShareLogin.this.finish();
						}
					
				  
				}
	        });
	        
	        web.setWebChromeClient(new WebChromeClient(){ 
	        	public void onProgressChanged(WebView view,int newProgress){	        		 
	        		 super.onProgressChanged(view, newProgress);
	        		 myProgressBar.setProgress(newProgress);
	        		 if(islongined)
		            		web.setVisibility(View.GONE);
	            } 
	        	 
	        });
	        
	        web.loadUrl(urls); 
	 } 
	 public void onClick(View v) {
			switch(v.getId()){
			    case R.id.back:
			    	finish();			    	
			    	break;	           					
		    }
		}
}

	 